﻿using EA.BA;
using EA.Common;
using EA.DTO;
using EAWebAdmin.template;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EAAdmin
{
    public partial class CreateCustomer : WebPage
    {
        override
        protected void Load(object sender, EventArgs e)
        {
            String pEmail = Request.Form["pEmail"];
            String pPassword = Request.Form["pPassword"];
            String pFullName = Request.Form["pFullName"];
            String pAddress = Request.Form["pAddress"];
            String pPhone = Request.Form["pPhone"];
            int pSellable = Convert.ToInt32(Request.Form["pSellable"]);
            if (pEmail != null && pPassword != null && pFullName != null && pAddress != null && pPhone != null)
            {
                CustomerBA customerBA = new CustomerBA();
                Boolean temp = true;
                String err = "";
                if ("".Equals(pEmail))
                {
                    err += "Email can not be empty<br>";
                    temp = false;
                }
                else
                {
                    CustomerTO customerTO = customerBA.GetCustomerByEmail(pEmail);
                    if (customerTO.Email != null)
                    {
                        err += "Email exists.<br>";
                        temp = false;
                    }              
                }
                if ("".Equals(pPassword))
                {
                    err += "Password can not be empty<br>";
                    temp = false;
                }              
                if ("".Equals(pFullName))
                {
                    err += "Full name can not be empty<br>";
                    temp = false;
                }
                if ("".Equals(pAddress))
                {
                    err += "Adress can not be empty<br>";
                    temp = false;
                }
                if ("".Equals(pPhone))
                {
                    err += "Phone can not be empty<br>";
                    temp = false;
                }
               
               
                if (temp)
                {
                    Boolean stt = false;
                    CustomerTO customer = new CustomerTO();
                    customer.Email = pEmail;
                    customer.Password = Md5.md5(pPassword);
                    customer.FullName = pFullName;
                    customer.Sellable = pSellable;
                    customer.Phone = pPhone;
                    customer.Address = pAddress;
                    stt = customerBA.InsertCustomer(customer);
                    if (stt == true)
                    {
                        Attributes.Add("success", "Insert success");
                        return;
                    }
                    else
                    {
                        Attributes.Add("err", "Insert faulty");
                        return;
                    }
                }
                else
                {
                    Attributes.Add("err", err);
                }
            }
        }
    }
}